package com.kexinde.wjifen.action;

import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import com.kexinde.wjifen.util.ValidateCode;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class ValidateCodeAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ByteArrayInputStream inputStream;

	public String execute() throws IOException {

		ValidateCode vc = new ValidateCode(100,26,4);
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		ImageOutputStream imageOut;
		imageOut = ImageIO.createImageOutputStream(output);
		ImageIO.write((RenderedImage) vc.getImage(), "JPEG", imageOut);
		imageOut.close();
		ByteArrayInputStream input = new ByteArrayInputStream(
				output.toByteArray());
		this.setInputStream(input);
		ActionContext.getContext().getSession().put("code", vc.getCode()); 
		return SUCCESS;
	}

	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

}
